//
//  NSObject+SY.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/5/2.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "NSObject+SY.h"

@implementation NSObject (SY)
-(NSString *)convertToJsonData:(NSDictionary *)dict {
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
//    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
//
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}
//date转换成字典
- (NSDictionary *)convertJsonStringToNSData:(NSData *)jsonData {
    
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        return nil;
    }
    return json;
}
//字符串转换成字典
- (NSDictionary *)convertJsonString:(NSString *)jsonStr {
    if (jsonStr == nil) {
        return nil;
    }
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    
    return [self convertJsonStringToNSData:jsonData];
}

//yanzheng
+ (BOOL)checkPassword:(NSString *) password {
    NSString *pattern = @"{6,12}";
    //    NSString *pattern = @"{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}
//延迟执行
+ (void)executeRunloop:(void (^)())runloop afterDelay:(float)delay {
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if (runloop) {
            runloop();
        }
    });
}
//保存数据
void SaveDataToNSUserDefaults(id data, NSString *key){
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
//得到数据
id GetDataFromNSUserDefaults(NSString *key) {
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
    
}
//删除所在的key的数据
void DeleteDataFromNSUserDefaults(NSString * key) {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSDate *)dateFormatter:(NSString *)time {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *FinalDay = [dateFormatter dateFromString:time];
    return FinalDay;
}

//日期对比
+ (int)compareOneDay:(NSDate *)currentDay withAnotherDay:(NSDate *)BaseDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDayStr = [dateFormatter stringFromDate:currentDay];
    NSString *BaseDayStr = [dateFormatter stringFromDate:BaseDay];
    NSDate *dateA = [dateFormatter dateFromString:currentDayStr];
    NSDate *dateB = [dateFormatter dateFromString:BaseDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", currentDay, BaseDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;//日期之前
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;//日期之后
    }
    //NSLog(@"Both dates are the same");
    return 0; //当天
}
//TOP - 白板1.0
//获取白板数组
/*
+ (NSMutableArray *)baibanZuArr:(NSArray *)bbzArr boardArrId:(NSArray *)boardArr {
    NSMutableArray *dataArr = [NSMutableArray array];
    
    NSMutableArray *fidsArr = [NSMutableArray array];
    
    for (TXBoardFileInfo *info in bbzArr) {//bbzarr:白板数组，包含文档，普通数组
        NSString *fid = info.fid;
        if ([info.title isEqualToString:@"普通白板"]) {
            
            
            for (NSString *fids in boardArr) {//单个取出所有白板得id，判断与白板组是否相同，如果相同存放进z数组当
                if ([fids containsString:fid]) {
                    SYInteractionModel *interaction = [[SYInteractionModel alloc] init];
                    interaction.fid = fids;
                    interaction.type = SYInteractionModelTypeOrdinaryBaiBan;//普通白板
                    [dataArr addObject:interaction];
                    [fidsArr addObject:fids];
                }
            }
        } else {//白板组
            
            SYInteractionModel *interaction = [[SYInteractionModel alloc] init];
            interaction.fidArr = [NSMutableArray array];
            interaction.type = SYInteractionModelTypeRestBaiBan;//其他样式
            for (NSString *fids in boardArr) {//单个取出所有白板得id，判断与白板组是否相同，如果相同存放进z数组当
                if ([fids containsString:fid]) {
                    SYInteractionModel *interaction1 = [[SYInteractionModel alloc] init];
                    interaction1.fid = fids;
                    interaction1.type = SYInteractionModelTypeRestBaiBan;//普通白板
                    [interaction.fidArr addObject:interaction1];
                    
                }
            }
            [dataArr addObject:interaction];
            if (interaction.fidArr.count > 0) {
                SYInteractionModel *fidsModel = interaction.fidArr.firstObject;
//                NSString *onefids = fidsModel.fid;
                
                interaction.oneFids = fidsModel.fid;
                [fidsArr addObject:fid];
            }
        }
    }
    
    NSMutableArray *dataListArr = [NSMutableArray array];
    //模型数组
    [dataListArr addObject:dataArr];
    //位置数组
    [dataListArr addObject:fidsArr];
    
    return dataListArr;
}
*/

@end





















